package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tongdun.TongdunZhiXinFenRecord;
import com.ucredit.babel.common.model.tongdun.TongdunZhiXinFfenData;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 同盾智信分-大额现金贷分
 *
 * @author xj
 */
public class TongdunZhiXinFenParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 同盾智信分-大额现金贷分
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        TongdunZhiXinFenRecord obj = (TongdunZhiXinFenRecord) model;
        TongdunZhiXinFfenData scoreData = obj.getData();
        JSONObject info = JSON.parseObject(scoreData.getData());
        JSONObject data = info.getJSONObject("result_desc").getJSONObject("ANTIFRAUD_MODEL").getJSONObject("26e96d7e6ada1e66");
        StringBuilder sb = getBaseInfo(obj, successful);
        //是否调用成功
        sb.append(info.getString("success")).append(CRT001);
        //进件ID
        sb.append(info.getString("id")).append(CRT001);
        //模型版本号
        sb.append(data.getString("model_version")).append(CRT001);
        //大额现金贷分值
        sb.append(data.getString("large_cash_score")).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tongdunzhixinfen_largecash_inc";
    }

    @Override
    public String getType() {
        return RecordType.TONGDUN_ZHIXINFEN_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "同盾智信分大额现金贷分";
    }

}
